/**
 * @param {number} n
 * @param {number[][]} edges
 * @return {number}
 */
var countPairs = function(n, edges) {
  let map = new Map(), visited = new Array(n).fill(false), result = [], index = 0, count = 0;
  for(let i = 0; i < n; i++) {
    map.set(i, [i]);
  }
  for(let i = 0; i < edges.length; i++) {
    map.get(edges[i][0]).push(edges[i][1]);
    map.get(edges[i][1]).push(edges[i][0]);
  }
  function dfs(i) {
    if(visited[i]) return;
    visited[i] = true;
    if(result[index]) result[index].push(i);
    else result[index] = [i];
    for(let j = 0; j < map.get(i).length; j++) {
      if(!visited[map.get(i)[j]]) {
        dfs(map.get(i)[j]);
      }
    }
  }
  for(let i = 0; i < n; i++) {
    if(!visited[i]) {
      dfs(i);
      index++;
    }
  }
  visited = new Array(n).fill(false);
  let sub = 0;
  for(let i = 0; i < result.length; i++) {
    count += (n - result[i].length - sub) * result[i].length;
    sub += result[i].length;
  }
  return count;
};
let n = 3, edges = [[0,1],[0,2],[1,2]];
console.log(countPairs(n, edges));
n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]];
console.log(countPairs(n, edges));
n = 12, edges = [];
console.log(countPairs(n, edges));
n = 12, edges = [[2,6],[11,3],[5,4],[9,6]];
console.log(countPairs(n, edges));
